package cn.com.headfree.loginmes.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.util.IdGen;
import com.gta.edu.sdk.util.StringUtil;

import cn.com.headfree.entity.LoginMes;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.loginmes.dao.ILoginMesDao;
import cn.com.headfree.vo.file.DealFileVo;

@Repository("loginMesDao")
public class LoginMesDaoImpl extends BaseDaoImpl<LoginMes> implements ILoginMesDao {

	@Override
	public void addLoginMes(User user, String loginIp) {
		String sql = "insert into t_login_mes (id,erp_no,user_name,base_org_id,base_org_name,login_ip,login_time) values ";
		StringBuilder sqlBuilder = new StringBuilder(sql);
		sqlBuilder.append("(#{id,jdbcType=VARCHAR},").append("#{erp_no,jdbcType=VARCHAR},").append("#{user_name,jdbcType=VARCHAR},")
		.append("#{base_org_id,jdbcType=VARCHAR},").append("#{base_org_name,jdbcType=VARCHAR},").append("#{login_ip,jdbcType=VARCHAR},").append("#{login_time,jdbcType=DATE})");
				
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", IdGen.uuid());
		params.put("erp_no", user.getFax());
		params.put("user_name", user.getName());
		params.put("base_org_id", user.getBaseOrgId());
		params.put("base_org_name", user.getBaseOrgName());
		params.put("login_ip", loginIp);
		params.put("login_time", new Timestamp(new Date().getTime()));
		this.executeSql(sqlBuilder.toString(), params);
	}

	@Override
	public List<LoginMes> getAllLoginTimes() {
		StringBuilder sqlBuilder = new StringBuilder(
				"select id,erp_no,user_name,base_org_id,base_org_name,login_ip,login_time ");
		sqlBuilder.append(" from t_login_mes where 1=1 ");
		ArrayList<Object> arrayList = new ArrayList<Object>();
//		if (!StringUtil.isNullOrEmpty(businessId)) {
//			sqlBuilder.append(" and business_id=?");
//			arrayList.add(businessId);
//		}
//		sqlBuilder.append(" order by order_num ");
		return this.findAllBySQL(LoginMes.class, sqlBuilder.toString(), null, false,arrayList.toArray());
	}

}
